/*
 * shdr.h
 *
 *  Created on: Sep 9, 2012
 *      Author: aortegag
 */

#ifndef SHDR_H_
#define SHDR_H_

#include "elf_t.h"

/**
 *  \brief Checks the ELF header if there is a Section Header table and
 *  and allocates e_shentsize*e_shnum bytes for the table.
 *
 *  \retval A pointer to an array for the section header table or 0 when there
 *  is no table.
 */
Elf64_Shdr *allocate_section_header_table(const Elf64_Ehdr *ehdr);

/**
 * \brief Reads the section header table from the file descriptor and saves it
 * into the shdr Section header table.
 *
 * \note This function assumes file pointed to by fp is already opened and that
 * shdr already points to a buffer of (ehdr->e_shentsize*ehdr->e_shnum) bytes.
 *
 * \param[in] fp File pointer to a valid file.
 * \param[in] ehdr ELF header table structure
 * \param[out] shdr New Section header table allocated.
 *
 * \retval Elf code
 */
elf_code_t read_section_header_table(FILE *fd, const Elf64_Ehdr *ehdr, Elf64_Shdr *shdr);

//! \brief Pretty prints the Section Header table
void	pretty_print_shdr(FILE *fd, const Elf64_Ehdr *ehdr, const Elf64_Shdr *shdr);


/**
 * \brief Gets a pointer to the section which matches section_name
 *
 * \param[in] fd ELF File descriptor
 * \param[in] list First element of the Section Header Table
 * \param[in] sh_num Number of Section Header entries in the table.
 * \param[in] section_name Section name.
 *
 * \retval Pointer to valid Section header, 0 on failure.
 */
const Elf64_Shdr *get_section_header_by_name(FILE *fd, const Elf64_Shdr *table,\
								const Elf64_Half sh_num, const char* section_name);

/**
 * \brief Gets a pointer to the section which matches section_name
 *
 * \param[in] fd ELF File descriptor
 * \param[in] list First element of the Section Header Table
 * \param[in] sh_num Number of Section Header entries in the table.
 *
 * \retval Pointer to valid Section header string table, 0 on failure.
 */
const Elf64_Shdr *get_section_header_string_table(FILE *fd, const Elf64_Shdr *table,\
								const Elf64_Half sh_num);

/**
 * \brief Gets a pointer to an array containing the section header names.
 *
 * \note This functions allocates memory for that array for the first time is
 * calles.
 *
 * \retval Pointer to an array.
 */
const char * get_section_header_names(FILE *fd, const Elf64_Shdr *table,\
		const Elf64_Half sh_num);

/**
 * \note We could implement this one, but there are several sections which are
 * the same type, in that case we would need to know which one we should return.
 * Getting the section header by name is better.
 */
Elf64_Shdr *get_section_header_by_type(FILE *fd, const Elf64_Shdr *list,\
										Elf64_Word sh_type);

/**
 * \brief Frees the memory allocated for section header names.
 *
 * \warning This must be called.
 */
void	cleanup_section_names();

void	print_section_contents(FILE *fd, const Elf64_Shdr *shdr);

void	print_all_section_contents(FILE *fd,\
		const Elf64_Half sh_num, const Elf64_Shdr *table);
#endif /* SHDR_H_ */
